
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rt.schedulebase.dto.DictHospitalDto;
import com.rt.schedulebase.dto.ExamScheduleActDto;
import com.rt.schedulebase.entity.DictHospital;
import com.rt.schedulebase.entity.ExamScheduleAct;
import com.rt.schedulebase.mapper.ExamScheduleActMapper;
import com.rt.schedulenew.api.IExamScheduleActService;
import com.rt.schedulenew.utils.base.CommonUtil;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.JsonResultUtil;
import com.rt.schedulenew.utils.global.PageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ExamScheduleActServiceImpl extends ServiceImpl<ExamScheduleActMapper, ExamScheduleAct> implements IExamScheduleActService {
    @Autowired
    DictHospitalServiceImpl dictHospitalService;

    @Override
    public JsonResult examScheduleActList(ExamScheduleActDto dto) {
        Page<?> page = PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        if (StringUtils.isNotBlank(dto.getExamClass())) {
            dto.setExamClassList(CommonUtil.split2List(dto.getExamClass()));
        }

        if (StringUtils.isNotBlank(dto.getActType())) {
            dto.setActTypeList(CommonUtil.split2List(dto.getActType()));
            if (dto.getActTypeList().contains("8")) {
                dto.setExamClassList(null);
            }
        }
        if (StringUtils.isNotBlank(dto.getDateType())) {
            if ("1".equals(dto.getDateType())) {
                dto.setActFromDate(dto.getFromDate());
                dto.setActToDate(dto.getToDate());
            } else {
                dto.setScheduleFromDate(dto.getFromDate());
                dto.setScheduleToDate(dto.getToDate());
            }
        }
        dto.setQueueNameList(CommonUtil.split2List(dto.getQueueName()));
        dto.setTerminalTypeList(CommonUtil.split2List(dto.getTerminalType()));
        List<ExamScheduleActDto> list = baseMapper.getExamScheduleActList(dto);
        for (ExamScheduleActDto examScheduleActDto : list) {
            if (StringUtils.isNotBlank(examScheduleActDto.getHospitalCode())) {
                DictHospitalDto dictHospitalDto = new DictHospitalDto();
                dictHospitalDto.setHospitalCode(examScheduleActDto.getHospitalCode());
                List<DictHospital> hospitalList = dictHospitalService.selectList(dictHospitalDto);
                examScheduleActDto.setHospitalName(hospitalList.get(0).getHospitalName());
            }
        }
        PageVO pageVO = new PageVO();
        pageVO.setPageNo(dto.getPageNo());
        pageVO.setPageSize(dto.getPageSize());
        pageVO.setTotal(page.getTotal());
        pageVO.setPages(page.getPages());
        return JsonResultUtil.success("成功", list, pageVO);
    }

    @Override
    public int findCount(ExamScheduleActDto actDto) {
        Map<String, String> map = new HashMap<>();
        map.put("SCHEDULED_ID", actDto.getScheduleId());
        map.put("TERMINAL_TYPE", actDto.getTerminalType());
        map.put("ACT_TYPE", actDto.getActType());
        QueryWrapper<ExamScheduleAct> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(map, false);
        Integer resultCount = baseMapper.selectCount(queryWrapper);
        return (resultCount != null) ? resultCount : 0;
    }
}
